using System;
using System.Xml;

namespace FridayShopping.DataModel
{
	/// <summary>
	/// Represents a unique key that identifies a item in the data model
	/// </summary>
	public struct Key
	{
		private int _value;

		#region Constructors
		public Key(int _value)
		{
			this._value = _value;
		}
		#endregion

		#region Member Access
		public int Value
		{
			get
			{
				return _value;
			}
			set
			{
				_value = value;
			}
		}
		#endregion

		#region Serialization/Deserizlization
		public void Serialize(XmlTextWriter xmlWriter)
		{
			Serialize(xmlWriter, GetType().Name);
		}

		public void Serialize(XmlTextWriter xmlWriter, string elementName)
		{
			xmlWriter.WriteStartElement(elementName);

			xmlWriter.WriteElementString("value", XmlConvert.ToString(Value));

			xmlWriter.WriteEndElement();
		}

		static public Key DeserializeMembers(XmlNodeReader xmlReader)
		{
			Key key = new Key();
			xmlReader.ReadStartElement();
			key.Value = XmlConvert.ToInt32(xmlReader.ReadElementString());
			xmlReader.ReadEndElement();
			return key;
		}
		#endregion

		#region Operators
		// overload operator ==
		public static bool operator ==(Key a, Key b) 
		{
			return (a.Value == b.Value);
		}

		public static bool operator !=(Key a, Key b) 
		{
			return (a.Value != b.Value);
		}

		public override bool Equals(object obj)
		{
			return (Value == ((Key)obj).Value);
		}

		#endregion

		public Key GetNextKey()
		{
			Value++;
			return new Key(Value);
		}

		public override int GetHashCode()
		{
			return Value;
		}

		public override string ToString()
		{
			return Value.ToString ();
		}

		public bool Empty
		{
			get 
			{
				return (Value < 0);
			}
		}

	}
}
